<?php

/**
 * Created with IntelliJ IDEA.
 * @Auther: 张昶
 * @Date: 2022/01/24/11:12
 * @Description:
 */


declare (strict_types = 1);

namespace app\home\controller;
use app\BaseController;
use app\home\validate\MenuCheck;
use app\Request;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;


class Menu extends BaseController
{
    //菜单配置
    public function index()
    {
        if (request()->isAjax()){
            $menu = Db::name('admin_menu')->order('sort asc')->select();
            return  to_assign(0,'',$menu);
        }else{
            return View();
        }
    }

    /**
    * @Description  新增菜单
    * @Date   2022/1/24 15:42
    */
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if ($param['id'] > 0) {
                try {
                    validate(MenuCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['update_time'] = time();
                Db::name('admin_menu')->strict(false)->field(true)->update($param);
//                add_log('edit', $param['id'], $param);   //员工操作日志
            } else {
                try {
                    validate(MenuCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['create_time'] = time();
                $mid = Db::name('admin_menu')->strict(false)->field(true)->insertGetId($param);
                //自动为系统所有者管理组分配新增的菜单
                $group = Db::name('admin_group')->find(1);
                if (!empty($group)) {
                    $newGroup['id'] = 1;
                    $newGroup['menus'] = $group['menus'] . ',' . $mid;
                    Db::name('admin_group')->strict(false)->field(true)->update($newGroup);
//                    add_log('add', $mid, $param);   //员工操作日志
                }
            }
            // 删除后台菜单缓存
            clear_cache('admin_menu');
            return to_assign();
        } else {

            $id = isset($param['id']) ? $param['id'] : 0;
            $pid = isset($param['pid']) ? $param['pid'] : 0;
            if($id>0){
                $detail = Db::name('admin_menu')->where('id',$id)->find();
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            View::assign('pid', $pid);
            return view();
        }

    }


    /**
     * @Description  删除菜单
     * @Date   2022/1/25 15:26
     */
    public function delete()
    {
        $id = get_params('id');
        $count = Db::name('admin_menu')->where(['pid' => $id])->count();
        if ($count > 0) {
            return to_assign(1, '该菜单下还有子菜单，无法删除');
        }
        if (Db::name('admin_menu')->delete($id) !== false) {
            // 删除后台菜单缓存
            clear_cache('admin_menu');
//            add_log('delete', $id, []);
            return to_assign(0, '删除菜单成功');
        } else {
            return to_assign(1, '删除失败');
        }
    }














}